package com.itheima.entry.factory;


import com.itheima.entry.controller.IUserController;
import com.itheima.entry.dao.IUserDao;
import com.itheima.entry.service.IUserService;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

/*
工厂类,生产需要的对象
方法全静态,方法返值类型为接口名,返回值为接口的实现类
 */
public class UserFactroy {
    private static Properties prop= new Properties();
    static {

        try {
//            ClassLoader loader = ClassLoader.getSystemClassLoader();
            InputStream reader = ClassLoader.getSystemResourceAsStream("factory.properties");
            prop.load(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static IUserDao getDao() {
        IUserDao o = null;
        try {
            Class<?> daoClazz = Class.forName(prop.getProperty("IUserDao"));
            o = (IUserDao) daoClazz.newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return o ;
    }

    public static IUserService getService() {
//        return new UserServiceImpl();
        IUserService o = null;
        try {
            Class<?> clazz = Class.forName(prop.getProperty("IUserService"));
            o = (IUserService) clazz.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return o ;


    }

    public static IUserController getController() {
//        return new UserControllerImpl();
        IUserController o = null;
        try {
            Class<?> clazz = Class.forName(prop.getProperty("IUserController"));
            o = (IUserController) clazz.getConstructor().newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return o ;



    }
}
